#include "converter.h"

void print_file_open_error( const wchar_t *file_name )
{
	char buff[1024];
	CharToOem( file_name, buff );
	//or may use: WideCharToMultiByte( CP_OEMCP, 0, file_name, -1, buff, 1024, 0, 0 );
	string msg( "Couldn't open file: " );
	msg += buff;
	printf( "%s\n", msg.c_str() );
}

void convert( const wchar_t *img_name, const wchar_t *txt_name )
{	
	Bitmap img( img_name );
	// check if img open successful
	if ( Ok != img.GetLastStatus() )
	{
		print_file_open_error( img_name );
		return;
	}

	// get size of image
	UINT width = img.GetWidth();
	UINT height = img.GetHeight();

	ofstream txt( txt_name );
	// check if txt open successful
	if ( !txt )
	{
		print_file_open_error( txt_name );
		return;
	}
	
	// create map: color -> char
	static Symbols symbols;
	// create text
	for ( UINT y = 0; y < height; y++ )
	{
		string str;
		for ( UINT x = 0; x < width; x++ )
		{
			// getting color of pixel
			Color pxcolor;
			img.GetPixel( x, y, &pxcolor );

			// get char for this color
			str.push_back( symbols[pxcolor] );
		}
		txt << str << endl;
	}
}